package com.xysd.examproject.base;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;

import com.xysd.examproject.activity.HomePageActivity;
import com.xysd.examproject.util.ToastUtil;
import com.xysd.examproject.util.homekey.HomeWatcher;

/**
 * Created by donghui on 2017/6/14.
 */

public class BaseActivity extends FragmentActivity implements HomeWatcher.OnHomePressedListener {
    private HomeWatcher mHomeWatcher;


    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        //在setContentView之前添加,未添加的話home鍵監聽無效，設置窗體屬性
        this.getWindow().setFlags(0x80000000, 0x80000000);
    }

    @Override
    protected void onResume() {
        mHomeWatcher = new HomeWatcher(this);
        mHomeWatcher.setOnHomePressedListener(this);
//         注册广播
        mHomeWatcher.startWatch();
        super.onResume();
    }

//    @Override
//    protected void onPause() {
//        mHomeWatcher.setOnHomePressedListener(null);
//        // 注销广播
//        mHomeWatcher.stopWatch();
//        super.onPause();
//    }



    private void goActivity() {
        final Intent intent=new Intent(this, HomePageActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                    startActivity(intent);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    @Override
    public void onHomePressed() {
        goActivity();

        ToastUtil.show("home");
    }

    @Override
    public void onHomeLongPressed() {
        goActivity();
        ToastUtil.show("homeLong");
    }
    @Override
    public void onGlobal_actions() {
        goActivity();
        ToastUtil.show("onGlobal_actions");
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            // 监控返回键
            new AlertDialog.Builder(BaseActivity.this).setTitle("提示")
                    .setIconAttribute(android.R.attr.alertDialogIcon)
                    .setMessage("确定要退出吗?")
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }})
                    .setNegativeButton("取消", null)
                    .create().show();
            return false;
        } else if(keyCode == KeyEvent.KEYCODE_MENU) {
            // 监控菜单键
            ToastUtil.show("menu");
            return true;
        }else if (keyCode == KeyEvent.KEYCODE_HOME){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }




}
